<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../node_modules/lodash/lodash.min.js"></script>
  <style>
    #box {
      width: 500px;
      height: 260px;
      background-color: teal;
      margin: 100px auto;
    }
  </style>
</head>

<body>
  <div id="box"></div>
  <script>
    /* box.addEventListener('mousemove', function(e){
      const disX = e.pageX - this.offsetLeft
      const disY = e.pageY - this.offsetTop
  
      this.innerHTML = `x: ${disX}, y: ${disY}`
    }) */

    /* let timer = null
    box.addEventListener('mousemove', function (e) {
      clearTimeout(timer)
      // !只要滑动的频率在 500ms 以内就会反复清除定时器，就没有机会再走 setTimeout 里面的代码了
      // 防抖（debounce）：持续触发不执行，不触发的一段时间后才执行
      timer = setTimeout(() => {
        const disX = e.pageX - this.offsetLeft
        const disY = e.pageY - this.offsetTop

        this.innerHTML = `x: ${disX}, y: ${disY}`
      }, 500)
    }) */

    /* function debounce(callback, time) {
      let timer = null
      return function(e){
        clearTimeout(timer)
        // e => 事件对象
        timer = setTimeout(() => {
          // callback(e) // window.callback(e)
          callback.call(this, e) // window.callback(e)
        }, time)
      }
    } */

    /* function debounce(callback, time) {
      let timer = null
      return function(){
        clearTimeout(timer)
        // e => 事件对象
        timer = setTimeout(() => {
          callback.apply(this, arguments)
        }, time)
      }
    } */

    box.addEventListener('mousemove', _.debounce(function (e) {
      const disX = e.pageX - this.offsetLeft
      const disY = e.pageY - this.offsetTop
      this.innerHTML = `x: ${disX}, y: ${disY}`
    }, 500))

    // 是什么
    // 怎么封装
    // 实际开发怎么用 lodash 的 debounce
  </script>
</body>

</html>